from system.object import Object
from system.eventServer import eventServer

class PullDown(Object):
    def __init__(self, parent, type = None, name = None):
        Object.__init__(self, parent, type, name)
        
        self.defaultVar("options",[])
        self.defaultVar("color",(0.75,0.75,0.75))
        self.defaultVar("arrowColor",(0.5,0.5,0.5))
        self.defaultVar("pos",(0,0))
        self.defaultVar("scale",(0.1,0))
        self.defaultVar("pullDownSize",0.1)
        self.defaultVar("direction","down")
        #self.defaultVar("renderOp2d","sceneNode")
        
        assert self.getVar("options"), "pullDown must have options"
        
        self.setVar("expanded",False)
        
        self.buttons = self.addChild("buttons")
        
        selection = self.getVar("options")[0]
        self.setVar("selection",selection)
        
        self.head = None
        
        self.setSelection(selection)
        
        eventServer.register("buttonClicked",self.buttonClicked)
        
    def makeArrow(self):
        self.arrow = self.head.addChild("arrow")
        self.arrow.setVar("renderOp2d","custom")
        self.arrow.setVar("renderType","triangles")
        self.arrow.setVar("pos",(0.9,0))
        self.arrow.setVar("scale",(0.1,1))
        if self.getVar("direction") == "down":
            self.arrow.setVar("points",[(0.1,0.1),(0.9,0.1),(0.5,0.9)])
        else:
            self.arrow.setVar("points",[(0.1,0.1),(0.9,0.5),(0.1,0.9)])
        self.arrow.setVar("color",self.getVar("arrowColor"))
        
    def setSelection(self, selection):
        if self.head:
            self.removeChild(self.head.getVar("name"))
        
        if self.hasVar("titleText"):
            text = self.getVar("titleText")
        elif self.hasVar("prefix"):
            text = "%s: %s" % (self.getVar("prefix"), selection)
        else:
            text = selection
        
        self.childLoadData.append({"vars":{"color":self.getVar("color"),"text":text,"pos":self.getVar("pos"),"scale":self.getVar("scale")}})
        self.head = self.addChild("gui.button","head")
        self.makeArrow()
        
        self.setVar("selection",selection)
        
        if self.hasVar("selectionEvent"):
            msgArgs = {"selection":selection,"path":self.getVar("path")}
            if self.hasVar("selectionEventArgs"):
                msgArgs.update(self.getVar("selectionEventArgs"))
            eventServer.pull(self.getVar("selectionEvent"),msgArgs)
        else:
            eventServer.pull("pullDownSelect",{"path":self.getVar("path"),"selection":selection})
        
    def expand(self):
        if not self.getVar("expanded"):
            self.setVar("expanded",True)
            
            options = self.getVar("options")
            size = self.getVar("pullDownSize")
            px, py = self.getVar("pos")
            sx, sy = self.getVar("scale")
            
            for i, option in enumerate(options):
                r = float(i) / float(len(options))
                
                if self.getVar("direction") == "down":
                    self.buttons.childLoadData.append({"vars":{"pos":(px,py+sy+ size*i),"scale":(sx,size),"text":option}})
                else: # right
                    self.buttons.childLoadData.append({"vars":{"pos":(px+sx,py + size*i),"scale":(sx,size),"text":option}})
                button = self.buttons.addChild("gui.button",option)
            
    def collapse(self):
        if self.getVar("expanded"):
            self.setVar("expanded",False)
            self.buttons.clearChildren()
        
    def buttonClicked(self, cmd, args):
        if args['path'] == self.head.getVar("path"):
            if not self.getVar("expanded"):
                self.expand()
            else:
                self.collapse()
                
        clear = False
        for button in self.buttons.children.itervalues():
            if args['path'] == button.getVar('path'):
                self.setSelection(button.getVar('text'))
                clear = True
        if clear:
            self.collapse()
